<template>
    <div id="user">
        <div style="margin: auto; ">
            <span style="font-size: larger;font-weight: 1000;">用户管理数据列表</span>
            <el-button type="primary" @click="addUser">添加</el-button>
            <el-dialog title="上传用户信息" :visible.sync="dialogFormVisible">
                <el-form :model="form" style="width: 70%;">
                    <el-form-item label="姓名" :label-width="formLabelWidth">
                        <el-input v-model="form.userName" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="电话号码" :label-width="formLabelWidth">
                        <el-input v-model="form.telephone" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="邮箱" :label-width="formLabelWidth">
                        <el-input v-model="form.email" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="地址" :label-width="formLabelWidth">
                        <el-input v-model="form.address" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item :label-width="formLabelWidth">
                        <el-button type="primary" v-if="isAdd" @click="addForm('form')" style="width:80%">提交</el-button>
                        <el-button type="primary" v-else="isAdd" @click="changeuserList()" style="width:80%">修改</el-button>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
                </div>
            </el-dialog>
        </div>
        <el-row style="margin: 10;">
            <el-button id="scope.row.useId" type="primary" @click="searchuserList()">查找</el-button>
            <el-col :span="6" style=" margin-left: 45vw;width: 270px; hight: 40px;">
                <el-input type="text" prefix-icon="el-icon-search" v-model="search" placeholder="请输入"
                    style="width: 270px; cursor: pointer"></el-input>
            </el-col>
        </el-row>
        <el-table :data="userList" style="width: 90%">
            <el-table-column prop="userName" label="用户姓名" width="180">
            </el-table-column>
            <el-table-column prop="telephone" label="电话" width="180">
            </el-table-column>
            <el-table-column prop="email" label="邮箱">
            </el-table-column>
            <el-table-column prop="address" label="地址">
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button id="scope.row.userId" type="primary" @click="edituserList(scope.row)">编辑</el-button>
                    <el-button id="scope.row.userId" type="danger" @click="deluserList(scope.row.userId)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="block">
            <el-pagination @current-change="handleCurrentChange" :page-size="5" :current-page="currentPage"
                layout=" prev, pager, next" :total="length">
            </el-pagination>
        </div>
    </div>
</template>
  
<script>
import myaxios from '../components/axios';
export default {
    data() {
        return {
            userList: [],
            filesPage: [],
            dialogFormVisible: false,
            form: {
                userId: '',
                userName: '',
                telephone: '',
                email: '',
                address: ''
            },
            isAdd: true,
            formLabelWidth: '100px',
            search: '',
            length: 5,
            total: 5,
            size: 5,
            currentPage: 1
        };
    },
    methods: {
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => {
                    done();
                })
                .catch(_ => { });
        },
        handleCurrentChange(val) {
            this.currentPage = val;
            console.log(this.length);
            this.getuserList();
        },
        addUser() {
            this.dialogFormVisible = true;
            this.isAdd = true;
            this.form.userId = '';
            this.form.userName = '';
            this.form.telephone = '';
            this.form.email = '';
            this.form.address = '';
        },
        edituserList(row) {
            this.dialogFormVisible = true;
            console.log(row);
            this.form.userId = row.userId;
            this.form.userName = row.userName;
            this.form.telephone = row.telephone;
            this.form.email = row.email;
            this.form.address = row.address;
            this.isAdd = false;
        },
        async getuserList() {
            await myaxios.get("/userList", { params: { size: this.size, page: this.currentPage } })
                .then(res => {
                    this.userList = res.data.userList;
                    this.length = res.data.length;
                })
        },
        async addForm() {
            await myaxios.post("/addUser",
                { userName: this.form.userName, telephone: this.form.telephone, email: this.form.email, address: this.form.address })
                .then(res => {
                    console.log(this.form)
                    if (res.data.code == 200) {
                        this.$message({
                            message: '添加成功！',
                            type: 'success'
                        })
                        this.userList = res.data.userList;
                    }
                }).catch(error => {
                    this.$message.error('添加失败！');
                })
            this.getuserList();
        },
        async changeuserList() {
            await myaxios.post("/changeUser",
                { userId: this.form.userId, userName: this.form.userName, telephone: this.form.telephone, email: this.form.email, address: this.form.address })
                .then(res => {
                    if (res.data.code == 200) {
                        this.$message({
                            message: '修改成功！',
                            type: 'success'
                        })
                        this.userList = res.data.userList;
                    }
                }).catch(error => {
                    this.$message.error('修改失败！');
                })
            this.getuserList();
        },
        async deluserList(id) {
            await myaxios.delete("/delUser" + id)
                .then(res => {
                    this.userList = res.data;
                    this.$message({
                        message: '删除成功',
                        type: 'success'
                    });
                }).catch(error => {
                    this.$message.error('删除失败！');
                })
            // this.getuserList();
        },
        async searchuserList() {
            if (this.search) {
                await myaxios.get("/searchUser", { params: { content: this.search } })
                    .then(res => {
                        console.log(res);
                        this.userList = res.data.userList;
                        this.length = this.userList.length;
                    })
            } else {
                this.getuserList();
            }
        }
    },
    mounted() {
        this.getuserList();
    }
};
</script>
  
<style>
#user {
    display: flex;
    justify-content: space-between;
    flex-direction: column;
    align-items: center;
}
</style>
  